import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "../views/Home/Home.vue"
import Order from "../views/Order/Order.vue"
import User from "../views/User/User.vue"
import UserMobile from "../views/UserMobile/UserMobile.vue"
import Login from "../views/Login/Login.vue"
import UserInfo from "../views/UserInfo/UserInfo.vue"
import Search from "../views/Screen/Search.vue"
import GoodsList from "../views/GoodsList/GoodsList.vue";
import GoodDesc from "../views/GoodDesc/GoodDesc.vue";
import Collection from "../views/Collection/Collection.vue";
import ConOrder from "../views/ConOrder/ConOrder.vue";
import PayMode from "../views/PayMode/PayMode.vue";
import Email from "../views/Email/Email.vue";
import MessageList from "../views/Email/components/EmailMessageList.vue";
import EmailDesc from "../views/Email/components/EmailDesc.vue";
import Appeal from "../views/Appeal/Appeal.vue";
import MyAccount from "../views/MyAccount/MyAccount.vue";
import Seeing from "../views/Seeing/Seeing.vue";
import Wallet from "../views/Wallet/Wallet.vue";

// 错误显示组件
import NetErr from "../views/common/NetErr.vue"
import NotFind from "../views/common/NotFind.vue"
import Axios from 'axios'

Vue.use(VueRouter)

const routes = [
  {path:"/",redirect:'/home'},
  {path:'/home',component:Home},
  {path:'/orders',component:Order,meta:{needLogin:true}},
  {path:'/user',component:User},
  {path:'/usermobile',component:UserMobile,meta:{needLogin:true}},
  {path:'/login',component:Login},
  {path:"/userinfo",component:UserInfo,meta:{needLogin:true}},
  {path:"/search",component:Search},
  {path:"/goodslist",component:GoodsList},
  {path:"/gooddesc",component:GoodDesc},
  {path:'/collection',component:Collection,meta:{needLogin:true}},
  {path:'/conorder',component:ConOrder,meta:{needLogin:true}},
  {path:'/paymode',component:PayMode,meta:{needLogin:true}},
  {path:'/email',component:Email,meta:{needLogin:true}},
  {path:"/messagelist",component:MessageList,meta:{needLogin:true}},
  {path:"/emaildesc",component:EmailDesc,meta:{needLogin:true}},
  {path:"/appeal",component:Appeal,meta:{needLogin:true}},
  {path:"/myaccount",component:MyAccount,meta:{needLogin:true}},
  {path:"/seeing",component:Seeing,meta:{needLogin:true}},
  {path:"/wallet",component:Wallet,meta:{needLogin:true}},
  // 错误路由
  {path:'/neterr',component:NetErr},
  {path:'*',component:NotFind},
]

const router = new VueRouter({
  routes
})

// 配置 router 拦截
// 前置
router.beforeEach((to,from,next) => {
  window.sessionStorage.setItem("chooes_bott",to.path);
  if(to.meta.needLogin){// 如果当前页面需要登录
    console.log("check token");
    // 检查token
    Axios.post("checkToken").then(succ => {
      if(succ.data.isok){// token有效
        next();
      }else{
        router.push("/login");
      }
    }).catch(err => {
      router.push("/login");
    })
  }else{
    next();
  }
});

// 后置
router.afterEach((to,from, next) => { 
  window.scrollTo(0,0)
})


export default router
